package servlet;
import bean.Admin;
import dao.impl.AdminDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/loginServe")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//请求编码
        resp.setContentType("text/html;charset=utf-8");//响应编码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminDaoImpl adminServlet = new AdminDaoImpl();
        Admin login = adminServlet.login(username, password);
        HttpSession session = req.getSession();
        if (login != null){
            session.setAttribute("user",login); //存储到缓存
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }else{
            session.setAttribute("msg","账号或密码错误！"); //存储到缓存
            resp.sendRedirect("/err/error.jsp");
        }
    }
}
